package com.qf.controller;

import com.qf.pojo.User;
import com.qf.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController("/user")
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("/findUserAll")
    public List<User> findAll(){
        return userService.findAll();
    }

    @RequestMapping("/findUserById")
    public User findById(@RequestBody Map map){
        Integer uid=(Integer)map.get("id");
        return userService.findById(uid);
    }

    @RequestMapping("/deleteById")
    public void delete(@RequestBody Map map){
        Integer uid=(Integer)map.get("id");
        userService.deleteById(uid);
    }
    @RequestMapping("/addAndUpdateUser")
    public String addAndUpdate(User user){
        User u = userService.addAndUpdate(user);
        if(u!=null){
            return "success";
        }else{
            return "fail";
        }
    }
    //注册
    @RequestMapping("/regist")
    public String regist(@RequestBody Map map){
        String phone=(String)map.get("phone");
        String pass=(String)map.get("pass");
        int i = userService.regist(phone, pass);
        if(i==1){
            return "success";
        }else{
            return "fail";
        }
    }
    @RequestMapping("/login")//登录
    public User login(@RequestBody Map map, HttpSession session){
        String phone=(String)map.get("phone");
        String pass=(String)map.get("pass");
        User user = userService.login(phone, pass);
        if(user!=null){
            session.setAttribute("user",user);
            return user;
        }else{
            return null;
        }
    }
    @RequestMapping("/logout")//注销
    public void logout(HttpSession session){
        session.invalidate();
        /*Subject subject = SecurityUtils.getSubject();
        subject.logout();*/
    }
}
